package org.apache.mina.example.lily.service;

import java.nio.charset.Charset;

import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFactory;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolEncoder;

public class Codec implements ProtocolCodecFactory{
	
	private ProtocolEncoder encoder;
	
	private ProtocolDecoder decoder;
	
	public Codec(boolean client) {
		if(client){
			  encoder = new ClientEncoder();
			  decoder = new ClientDecoder(Charset.forName("utf-8"));
		}else{
			encoder = new ServerEncoder();
			decoder = new ServerDecoder(Charset.forName("utf-8"));
		}
	}

	@Override
	public ProtocolEncoder getEncoder(IoSession session) throws Exception {
		return encoder;
	}

	@Override
	public ProtocolDecoder getDecoder(IoSession session) throws Exception {
		return decoder;
	}

}
